/*
 * User: lin
 * Date:
 * Describe:注册
 *
 */

package com.xinxin.facelinker.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.easemob.EMError;
import com.easemob.chat.EMChatManager;
import com.easemob.chatuidemo.R;
import com.easemob.exceptions.EaseMobException;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.xinxin.facelinker.Config;
import com.xinxin.facelinker.DemoApplication;
import com.xinxin.facelinker.domain.GetLogonVerificationCode;
import com.xinxin.facelinker.ui.button.views.ButtonRectangle;
import com.xinxin.facelinker.utils.NetHelper;

public class RegisterActivity extends Activity {

    public EditText etPhone_Num;
    private EditText etPassword;
    private EditText etPassword_Again;
    private EditText etCheck;
    private ButtonRectangle btnLogon;
    private HttpUtils httpUtils = new HttpUtils();
    private ButtonRectangle btnCheckCode;
    private GetLogonVerificationCode getLogonVerificationCode;
    String username;
    String pwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        etPhone_Num = (EditText) findViewById(R.id.etPhone_num);
        etPassword = (EditText) findViewById(R.id.etPassword);
        etPassword_Again = (EditText) findViewById(R.id.etPassword_Again);
        etCheck = (EditText) findViewById(R.id.etCheck);
        btnLogon = (ButtonRectangle) findViewById(R.id.btnLogon);
        btnCheckCode = (ButtonRectangle) findViewById(R.id.btnCheck);

        btnLogon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

//                if (etCheck.getText().toString().isEmpty() == true) {
//                    Toast.makeText(RegisterActivity.this, "未获取验证码", Toast.LENGTH_SHORT).show();
//                    return;
//                }

                if (etPhone_Num.getText().toString().length() != 11) {
                    Toast.makeText(RegisterActivity.this, R.string.phone_num_wrong, Toast.LENGTH_SHORT).show();
                    return;
                }

                if (etPassword.getText().toString().isEmpty() == true || etPassword_Again.getText().toString().isEmpty() == true) {
                    Toast.makeText(RegisterActivity.this, R.string.password_wrong, Toast.LENGTH_SHORT).show();
                    return;
                }

                if (!etPassword.getText().toString().equals(etPassword_Again.getText().toString())) {
                    Toast.makeText(RegisterActivity.this, R.string.password_again_wrong, Toast.LENGTH_SHORT).show();
                    return;
                }

//                try {
//                    if (!getLogonVerificationCode.getCode().equals(etCheck.getText().toString())) {
//                        Toast.makeText(RegisterActivity.this, R.string.verification_code_wrong, Toast.LENGTH_SHORT).show();
//                        return;
//                    }
//                } catch (Exception e) {
//                    Toast.makeText(RegisterActivity.this, "验证失败", Toast.LENGTH_SHORT).show();
//                }

//                final ProgressDialog pd = ProgressDialog.show(RegisterActivity.this, getResources().getString(R.string.logon_connecting), getResources().getString(R.string.logon_connecting_to_server));
//                RequestParams params = new RequestParams();
//                params.addBodyParameter(Config.ACTION, Config.ACTION_LOGON);
//                params.addBodyParameter(Config.KEY_PHONE_NUM, etPhone_Num.getText().toString());
//                params.addBodyParameter(Config.KEY_PASSWORD, etPassword.getText().toString());
//                httpUtils.send(HttpRequest.HttpMethod.POST, Config.CATEGORIES_URL, params, new RequestCallBack<String>() {
//                    @Override
//                    public void onSuccess(ResponseInfo<String> responseInfo) {
//                        pd.dismiss();
//                        Intent i = new Intent(RegisterActivity.this, AddInfoActivity.class);
//                        i.putExtra(Config.KEY_PHONE_NUM, etPhone_Num.getText().toString());
//                        startActivity(i);
//                        finish();
//                    }
//
//                    @Override
//                    public void onFailure(HttpException e, String s) {
//                        pd.dismiss();
//                        Toast.makeText(RegisterActivity.this, R.string.logon_fail, Toast.LENGTH_SHORT).show();
//                    }
//                });
                username = etPhone_Num.getText().toString();
                pwd = etPassword.getText().toString();
                if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(pwd)) {
                    final ProgressDialog pd = new ProgressDialog(RegisterActivity.this);
                    pd.setMessage(getResources().getString(R.string.Is_the_registered));
                    pd.show();

                    new Thread(new Runnable() {
                        public void run() {
                            try {
                                // 调用sdk注册方法
                                EMChatManager.getInstance().createAccountOnServer(username, pwd);
                                runOnUiThread(new Runnable() {
                                    public void run() {
                                        if (!RegisterActivity.this.isFinishing())
                                            pd.dismiss();
                                        // 保存用户名
                                        DemoApplication.getInstance().setUserName(username);
                                        Toast.makeText(getApplicationContext(), getResources().getString(R.string.Registered_successfully), Toast.LENGTH_SHORT).show();
                                        finish();
                                    }
                                });
                            } catch (final EaseMobException e) {
                                runOnUiThread(new Runnable() {
                                    public void run() {
                                        if (!RegisterActivity.this.isFinishing())
                                            pd.dismiss();
                                        int errorCode = e.getErrorCode();
                                        if (errorCode == EMError.NONETWORK_ERROR) {
                                            Toast.makeText(getApplicationContext(), getResources().getString(R.string.network_anomalies), Toast.LENGTH_SHORT).show();
                                        } else if (errorCode == EMError.USER_ALREADY_EXISTS) {
                                            Toast.makeText(getApplicationContext(), getResources().getString(R.string.User_already_exists), Toast.LENGTH_SHORT).show();
                                        } else if (errorCode == EMError.UNAUTHORIZED) {
                                            Toast.makeText(getApplicationContext(), getResources().getString(R.string.registration_failed_without_permission), Toast.LENGTH_SHORT).show();
                                        } else if (errorCode == EMError.ILLEGAL_USER_NAME) {
                                            Toast.makeText(getApplicationContext(), getResources().getString(R.string.illegal_user_name), Toast.LENGTH_SHORT).show();
                                        } else {
                                            Toast.makeText(getApplicationContext(), getResources().getString(R.string.Registration_failed) + e.getMessage(), Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                });
                            }
                        }
                    }).start();

                }

            }
        });

        btnCheckCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (etPhone_Num.getText().toString().isEmpty() == true) {
                    Toast.makeText(RegisterActivity.this, R.string.phone_num_wrong, Toast.LENGTH_SHORT).show();
                    return;
                }

                RequestParams params = new RequestParams();
                params.addBodyParameter(Config.ACTION, Config.ACTION_GET_LOGON_VERIFICATION_CODE);
                params.addBodyParameter(Config.KEY_PHONE_NUM, etPhone_Num.getText().toString());
                httpUtils.send(HttpRequest.HttpMethod.POST, Config.CATEGORIES_URL, params, new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        getLogonVerificationCode = (GetLogonVerificationCode) NetHelper.parseJsonData(responseInfo.result, GetLogonVerificationCode.class);
                        Toast.makeText(RegisterActivity.this, "获取验证码成功", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onFailure(HttpException e, String s) {
                        Toast.makeText(RegisterActivity.this, "获取验证码失败", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }

    public void back(View View) {
        finish();
    }

}

